package com.example.demo.nuoqi.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.example.demo.nuoqi.dao.InviteapplicationMapper;
import com.example.demo.nuoqi.dao.ItemdetailsMapper;
import com.example.demo.nuoqi.dao.ResumemanagementMapper;
import com.example.demo.nuoqi.pojo.*;
import com.example.demo.nuoqi.service.ApplicationService;
import com.example.demo.nuoqi.service.InviteapplicationService;
import com.example.demo.nuoqi.service.ItemdetailsService;
import com.example.demo.nuoqi.service.ResumemanagementService;
import com.example.demo.nuoqi.utils.JsonUtils;
import com.example.demo.nuoqi.utils.Result;
import com.example.demo.nuoqi.utils.StatusCode;
import com.example.demo.nuoqi.utils.UserUtils;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.builders.ApiInfoBuilder;

import javax.crypto.spec.IvParameterSpec;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/****
 * @Author:shenkunlin
 * @Description:
 * @Date 2019/6/14 0:18
 *****/
@Api(value = "InviteapplicationController")
@RestController
@RequestMapping("/inviteapplication")
@CrossOrigin
public class InviteapplicationController {

    @Autowired
    private InviteapplicationService inviteapplicationService;
    @Autowired
    ResumemanagementService resumemanagementService;
    @Autowired
    ResumemanagementMapper resumemanagementMapper;
    @Autowired
    ItemdetailsService itemdetailsService;
    @Autowired
    ItemdetailsMapper itemdetailsMapper;
    @Autowired
    ApplicationService applicationService;
    @Autowired
    InviteapplicationMapper inviteapplicationMapper;

    /***
     * Inviteapplication分页条件搜索实现
     * @param inviteapplication
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "Inviteapplication条件分页查询",notes = "分页条件查询Inviteapplication方法详情",tags = {"InviteapplicationController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false) @ApiParam(name = "Inviteapplication对象",value = "传入JSON数据",required = false) Inviteapplication inviteapplication, @PathVariable int page, @PathVariable int size){
        //调用InviteapplicationService实现分页条件查询Inviteapplication
        PageInfo<Inviteapplication> pageInfo = inviteapplicationService.findPage(inviteapplication, page, size);
        return new Result(true, StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * Inviteapplication分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "Inviteapplication分页查询",notes = "分页查询Inviteapplication方法详情",tags = {"InviteapplicationController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size){
        //调用InviteapplicationService实现分页查询Inviteapplication
        PageInfo<Inviteapplication> pageInfo = inviteapplicationService.findPage(page, size);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     *
     * @param inviteapplication
     * @return
     */
    @ApiOperation(value = "Inviteapplication条件查询",notes = "条件查询Inviteapplication方法详情",tags = {"InviteapplicationController"})
    @PostMapping(value = "/search" )
    public Result<List<Inviteapplication>> findList(@RequestBody(required = false) @ApiParam(name = "Inviteapplication对象",value = "传入JSON数据",required = false) Inviteapplication inviteapplication){
        //调用InviteapplicationService实现条件查询Inviteapplication
        List<Inviteapplication> list = inviteapplicationService.findList(inviteapplication);
        return new Result<List<Inviteapplication>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Inviteapplication根据ID删除",notes = "根据ID删除Inviteapplication方法详情",tags = {"InviteapplicationController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用InviteapplicationService实现根据主键删除
        inviteapplicationService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改Inviteapplication数据
     * @param inviteapplication
     * @param id
     * @return
     */
    @ApiOperation(value = "Inviteapplication根据ID修改",notes = "根据ID修改Inviteapplication方法详情",tags = {"InviteapplicationController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @PutMapping(value="/{id}")
    public Result update(@RequestBody @ApiParam(name = "Inviteapplication对象",value = "传入JSON数据",required = false) Inviteapplication inviteapplication, @PathVariable Integer id){
        //设置主键值
        inviteapplication.setId(id);
        //调用InviteapplicationService实现修改Inviteapplication
        inviteapplicationService.update(inviteapplication);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增Inviteapplication数据
     * @param inviteapplication
     * @return
     */
    @ApiOperation(value = "Inviteapplication添加",notes = "添加Inviteapplication方法详情",tags = {"InviteapplicationController"})
    @PostMapping
    public Result add(@RequestBody @ApiParam(name = "Inviteapplication对象",value = "传入JSON数据",required = true) Inviteapplication inviteapplication){
        //调用InviteapplicationService实现添加Inviteapplication
        inviteapplicationService.add(inviteapplication);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询Inviteapplication数据
     * @param id
     * @return
     */
    @ApiOperation(value = "Inviteapplication根据ID查询",notes = "根据ID查询Inviteapplication方法详情",tags = {"InviteapplicationController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @GetMapping("/{id}")
    public Result<Inviteapplication> findById(@PathVariable Integer id){
        //调用InviteapplicationService实现根据主键查询Inviteapplication
        Inviteapplication inviteapplication = inviteapplicationService.findById(id);
        return new Result<Inviteapplication>(true,StatusCode.OK,"查询成功",inviteapplication);
    }

    /***
     * 查询Inviteapplication全部数据
     * @return
     */
    @ApiOperation(value = "查询所有Inviteapplication",notes = "查询所Inviteapplication有方法详情",tags = {"InviteapplicationController"})
    @GetMapping
    public Result<List<Inviteapplication>> findAll(){
        //调用InviteapplicationService实现查询所有Inviteapplication
        List<Inviteapplication> list = inviteapplicationService.findAll();
        return new Result<List<Inviteapplication>>(true, StatusCode.OK,"查询成功",list) ;
    }


    /***
     * 邀请人加入项目
     * @return
     */
    @ApiOperation(value = "邀请人加入项目",notes = "邀请人加入项目",tags = {"InviteapplicationController"})
    @GetMapping("/inviteJoinItem/{itemId}/{resumeId}")
    public Result inviteJoinItem(@PathVariable("itemId")Integer itemId, @PathVariable("resumeId") Integer resumeId, HttpSession session){
        Itemdetails itemdetails = itemdetailsService.findById(itemId);
        Resumemanagement resumemanagement = resumemanagementService.findById(resumeId);
        User user = UserUtils.getUser(session);

        Inviteapplication inviteapplication = new Inviteapplication();
        inviteapplication.setItemName(itemdetails.getItemName());
        inviteapplication.setItemId(itemdetails.getId());
        inviteapplication.setInvitePerson(resumemanagement.getCreateId().toString());
        inviteapplication.setCreateTime(new Date());
        inviteapplication.setStatus("1");
        inviteapplication.setUserId(user.getId());
        inviteapplication.setUserName(user.getName());
        inviteapplicationMapper.insertSelective(inviteapplication);

        Integer insert = inviteapplication.getId();

        //设置被邀请审批
        Application application = new Application();
        application.setMyApplication(String.valueOf(insert));
        application.setApproveApplication(String.valueOf(itemdetails.getId()));
        application.setItemInvite(String.valueOf(user.getId()));
        application.setUserId(Integer.valueOf(inviteapplication.getInvitePerson()));
        application.setUserName(resumemanagement.getUserName());
        application.setStatus("1");


        applicationService.add(application);
        return new Result(true, StatusCode.OK,"邀请成功,等待回复") ;
    }



    /***
     * 设置审批结果
     * @return
     */
    @ApiOperation(value = "设置审批结果",notes = "设置审批结果方法详情",tags = {"InviteapplicationController"})
    @GetMapping("setInviteResult/{chooseId}/{result}")
    public Result setInviteResult(@PathVariable("chooseId") Integer id,@PathVariable("result")String result){
        Inviteapplication inviteapplication = inviteapplicationService.findById(id);



        Application entity = new Application();
        String myApplication = String.valueOf(inviteapplication.getId());
        entity.setMyApplication(myApplication);
        entity.setItemInvite(String.valueOf(inviteapplication.getUserId()));

        Itemdetails itemdetails = itemdetailsService.findById(inviteapplication.getItemId());




        Application application = applicationService.get(entity);



        application.setStatus(result);


        // 同意退出项目
        if ("6".equals(result)) {
            inviteapplication.setStatus(result);
            application.setStatus(result);


            // 遍历项目参与者
            List<Itemdetails.participantTemp> tempList = JsonUtils.jsonToList(itemdetails.getParticipant(), Itemdetails.participantTemp.class);
            List<Itemdetails.participantTemp> temp =new ArrayList<>();
            temp.addAll(tempList);
            for (Itemdetails.participantTemp participantTemp : tempList) {
                // true 说明有这个参与者
                if (participantTemp.getUserId() == application.getUserId()) {
                    temp.remove(participantTemp);
                }
            }

            if (temp.size() >= 0 && null != temp) {
                String json = JsonUtils.objectToJson(temp);
                itemdetails.setParticipant(json);
            }
            inviteapplicationService.update(inviteapplication);
            applicationService.update(application);
            itemdetailsService.update(itemdetails);
            return new Result(true, StatusCode.OK,"以同意退出",null) ;
        }




        inviteapplication.setStatus(result);
        inviteapplicationService.update(inviteapplication);
        return new Result(true, StatusCode.OK,"邀请审批完成",null) ;
    }



    /***
     * 查询当前用户邀请全部数据
     * @return
     */
    @ApiOperation(value = "查询当前用户邀请全部数据",notes = "查询当前用户邀请全部数据",tags = {"InviteapplicationController"})
    @GetMapping("findByUserInviteAll")
    public Result<List<Inviteapplication>> findByUserInviteAll(HttpSession session){

        User user = UserUtils.getUser(session);


        //调用InviteapplicationService实现查询所有Inviteapplication
        List<Inviteapplication> list = inviteapplicationService.findAll();
        List<Inviteapplication> temp = new ArrayList<>();

        for (Inviteapplication inviteapplication : list) {
            if (inviteapplication.getUserId() == user.getId()) {
                temp.add(inviteapplication);
            }
        }
        return new Result<List<Inviteapplication>>(true, StatusCode.OK,"查询成功",temp) ;
    }


    /***
     * 取消邀请
     * @return
     */
    @ApiOperation(value = "取消邀请",notes = "取消邀请",tags = {"InviteapplicationController"})
    @GetMapping("setCancelInvite/{chooseId}/{result}")
    public Result setCancelInvite(@PathVariable("chooseId") Integer id,@PathVariable("result")String result){
        Inviteapplication inviteapplication = inviteapplicationService.findById(id);


        Application entity = new Application();
        String myApplication = String.valueOf(inviteapplication.getId());
        entity.setMyApplication(myApplication);
        entity.setItemInvite(String.valueOf(inviteapplication.getUserId()));

        Application application = applicationService.get(entity);



        application.setStatus(result);

        inviteapplication.setStatus(result);
        inviteapplicationService.update(inviteapplication);
        applicationService.update(application);
        return new Result(true, StatusCode.OK,"取消邀请成功",null) ;
    }

}
